using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VLRPOS
    {
        public DataRow Convert2LRPOS(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryLRPOS(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["LRPMANDANT"] = " ".PadRight(2 ); 
         dwReturn["LRPREZART"] = " ".PadRight(1 ); 
         dwReturn["LRPREZNR"] = " ".PadRight(10 ); 
         dwReturn["LRPREZST"] = " ".PadRight(1 ); 
         dwReturn["LRPPOS"] = " ".PadRight(0 ); 
         dwReturn["LRPTYP"] = " ".PadRight(1 ); 
         dwReturn["LRPKOMP"] = " ".PadRight(14 ); 
         dwReturn["LRPKONZ"] = " ".PadRight(10 ); 
         dwReturn["LRPBEZUG"] = " ".PadRight(1 ); 
         dwReturn["LRPFLOTTE"] = " ".PadRight(2 ); 
         dwReturn["LRPVISKO"] = " ".PadRight(0 ); 
         dwReturn["LRPABRUF"] = " ".PadRight(0 ); 
         dwReturn["LRPCOPURE"] = " ".PadRight(6 ); 
         dwReturn["LRPZUSATZ"] = " ".PadRight(2 ); 
         dwReturn["LRPMETHODE"] = " ".PadRight(0 ); 
         dwReturn["LRPRAKELART"] = " ".PadRight(3 ); 
         dwReturn["LRPDRUCK"] = " ".PadRight(0 ); 
         dwReturn["LRPFUELLHOE"] = " ".PadRight(0 ); 
         dwReturn["LRPFILLER"] = " ".PadRight(33 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapLRPOS(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveLRPOS(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryLRPOS(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapLRPOS(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertLRPOS(dwLocal, ITXDBVERSION);
            }
            UpdateLRPOS(dwLocal);
        }
        public void InsertLRPOS(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO LRPOS(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryLRPOS(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM LRPOS WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM LRPOS  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "LRPOS";
            Return = Result;
            return Return;
        }
        public void DeleteLRPOS(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM LRPOS   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateLRPOS(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE LRPOS SET "
                    +"LRPMANDANT= :LRPMANDANT,"
                    +"LRPREZART= :LRPREZART,"
                    +"LRPREZNR= :LRPREZNR,"
                    +"LRPREZST= :LRPREZST,"
                    +"LRPPOS= :LRPPOS,"
                    +"LRPTYP= :LRPTYP,"
                    +"LRPKOMP= :LRPKOMP,"
                    +"LRPKONZ= :LRPKONZ,"
                    +"LRPBEZUG= :LRPBEZUG,"
                    +"LRPFLOTTE= :LRPFLOTTE,"
                    +"LRPVISKO= :LRPVISKO,"
                    +"LRPABRUF= :LRPABRUF,"
                    +"LRPCOPURE= :LRPCOPURE,"
                    +"LRPZUSATZ= :LRPZUSATZ,"
                    +"LRPMETHODE= :LRPMETHODE,"
                    +"LRPRAKELART= :LRPRAKELART,"
                    +"LRPDRUCK= :LRPDRUCK,"
                    +"LRPFUELLHOE= :LRPFUELLHOE,"
                    +"LRPFILLER= :LRPFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("LRPMANDANT", OracleType.NVarChar).Value = dwData["LRPMANDANT"].ToString(); 
          or1.Parameters.Add("LRPREZART", OracleType.NVarChar).Value = dwData["LRPREZART"].ToString(); 
          or1.Parameters.Add("LRPREZNR", OracleType.NVarChar).Value = dwData["LRPREZNR"].ToString(); 
          or1.Parameters.Add("LRPREZST", OracleType.NVarChar).Value = dwData["LRPREZST"].ToString(); 
          or1.Parameters.Add("LRPPOS", OracleType.NVarChar).Value = dwData["LRPPOS"].ToString(); 
          or1.Parameters.Add("LRPTYP", OracleType.NVarChar).Value = dwData["LRPTYP"].ToString(); 
          or1.Parameters.Add("LRPKOMP", OracleType.NVarChar).Value = dwData["LRPKOMP"].ToString(); 
          or1.Parameters.Add("LRPKONZ", OracleType.NVarChar).Value = dwData["LRPKONZ"].ToString(); 
          or1.Parameters.Add("LRPBEZUG", OracleType.NVarChar).Value = dwData["LRPBEZUG"].ToString(); 
          or1.Parameters.Add("LRPFLOTTE", OracleType.NVarChar).Value = dwData["LRPFLOTTE"].ToString(); 
          or1.Parameters.Add("LRPVISKO", OracleType.NVarChar).Value = dwData["LRPVISKO"].ToString(); 
          or1.Parameters.Add("LRPABRUF", OracleType.NVarChar).Value = dwData["LRPABRUF"].ToString(); 
          or1.Parameters.Add("LRPCOPURE", OracleType.NVarChar).Value = dwData["LRPCOPURE"].ToString(); 
          or1.Parameters.Add("LRPZUSATZ", OracleType.NVarChar).Value = dwData["LRPZUSATZ"].ToString(); 
          or1.Parameters.Add("LRPMETHODE", OracleType.NVarChar).Value = dwData["LRPMETHODE"].ToString(); 
          or1.Parameters.Add("LRPRAKELART", OracleType.NVarChar).Value = dwData["LRPRAKELART"].ToString(); 
          or1.Parameters.Add("LRPDRUCK", OracleType.NVarChar).Value = dwData["LRPDRUCK"].ToString(); 
          or1.Parameters.Add("LRPFUELLHOE", OracleType.NVarChar).Value = dwData["LRPFUELLHOE"].ToString(); 
          or1.Parameters.Add("LRPFILLER", OracleType.NVarChar).Value = dwData["LRPFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
